<%--
  Created by IntelliJ IDEA.
  User: head79
  Date: 2017/6/26
  Time: 22:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <script src="https://cdn.bootcss.com/jquery/2.1.3/jquery.js"></script>
    <script src="https://cdn.bootcss.com/jquery/2.1.3/jquery.min.js"></script>
    <title>Title</title>
    <script>
        /**
         * 全选
         *
         * allCkb 全选复选框的id
         * items 复选框的name
         */
        function allCheck(allCkb, items){
            $("#"+allCkb).click(function(){
                $('[name='+items+']:checkbox').attr("checked", this.checked );
            });
        }
        ////////添加一行、删除一行测试方法///////
        $(function(){
            //全选
            allCheck("allCkb", "ckb");
        });
        function addTr(tab, row, trHtml){
            var $tr=$("#"+tab+" tr").eq(row);
            if($tr.size()==0){
                alert("指定的table id或行数不存在！");
                return;
            }
            $tr.after(trHtml);
        }
        function delTr(ckb){
            //获取选中的复选框，然后循环遍历删除
            var ckbs=$("input[name="+ckb+"]:checked");
            if(ckbs.size()==0){
                alert("要删除指定行，需选中要删除的行！");
                return;
            }
            ckbs.each(function(){
                $(this).parent().parent().remove();
            });
        }
        function addTr2(tab, row){
            var trHtml="<tr align='center'><td width='30%'><input type='checkbox' name='ckb'/></td>" +
                "<td width='30%'><input type='text' id='subject' name='subject[]' value='数学'></td>" +
                "<td width='30%'><input type='text' id='source' name='source[]' value='80'></td>" +
                "</tr>";
            addTr(tab, row, trHtml);
        }
        function delTr2(){
            delTr('ckb');
        }
      /*  $(document).ready(function(){
            $("#sub").click(function(){
                var array=[];
                $("input[name^='subject']").each(function(i, o){
                    array[i] = $(o).val();
                    //  a.push($(o).val());
                    // 两种写法选一个就好
                });
                $.ajax({
                    data:{name:"ss"},
                    type:"post",
                    dataType: 'JSON',
                    url:"/webssm/showJson",
                    success:function(data){
                        alert("success:"+data);
                    }
                });
            });
        });*/
      var flag;
      function vaild() {
          $("input[name^='subject']").each(function(i, o){
              if($(o).val()==''){
                  alert("第"+(i+1)+"行科目不能为空！")
                  flag=false;
                  return false;
              }else{
                  flag=true;
                 return true;
              }
          });
          if(flag){
              subR();
          }
      }
      function subR() {
          var array=[];
          var array1=[];
          var object;
          $("input[name^='subject']").each(function(i, o){
              array[i] = $(o).val();
              //  array.push($(o).val());
              // 两种写法选一个就好
          });
          $("input[name^='source']").each(function(i, o){
              //array[i] = $(o).val();
              array1.push($(o).val());
              // 两种写法选一个就好
          });
          var arr=new Array();
          arr.push(array);
          arr.push(array1);
          $.ajax({
              data:{"subject":JSON.stringify(array),"source":JSON.stringify(array1)},
              type:"post",
              dataType: 'JSON',
              async: false,
              url:"/webssm/showJson",
              error:function(data){
                  alert("出错了！！:"+data.msg);
              },
              success:function(data){
                  alert("success:"+data.msg);
                  $("#result").html(data.msg) ;
              }
          });
      }
    </script>
</head>
<body>
<form action="">
<table  border="1px #ooo" id="tab" cellpadding="0"
        cellspacing="0" width="30%">
    <tr align="center">
        <td width="30%"><input id="allCkb" type="checkbox"/></td>
        <td width="30%">科目</td>
        <td width="30%">成绩</td>
    </tr>
    <input type="button" onclick="addTr2('tab', -1);" value="增">
    <input type="button" onclick="delTr2();" value="删">
    <input type="button" value="提交"onclick="vaild();" id="sub">
</table>



</form>
</body>
</html>
